package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.oj.ds.ListNode

/**
 * @author flutterdash@qq.com
 * @since 2021/4/6 21:37
 */
class P141 {

    private fun hasCycle(head: ListNode?): Boolean {
        if (head?.next == null)
            return false
        var slow = head
        var fast = head.next
        while (slow != fast) {
            if (slow == null || fast == null)
                return false
            slow = slow.next
            fast = fast.next?.next
        }
        return true
    }

    @Test
    fun test0() {
        val node1 = ListNode(3)
        val node2 = ListNode(2)
        val node3 = ListNode(0)
        val node4 = ListNode(-4)

        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node2

        println(hasCycle(node1))
    }

}
